/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReportGenFrame.java
 *
 * Created on Apr 6, 2014, 8:45:45 AM
 */

package propertymgmt.gui;

import java.awt.Font;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import propertymgmt.entities.Lease;
import propertymgmt.entities.Property;
import propertymgmt.entities.Tenant;
import propertymgmt.util.PropertiesManager;

/**
 * ReportGenFrame.java
 * 
 * Allows the user to view and create reports based on the
 * current Property objects within the database.
 *
 * @author Brian
 */
@SuppressWarnings("serial")
public class ReportGenFrame extends javax.swing.JInternalFrame {

	/** Creates new form ReportGenFrame */
	public ReportGenFrame() {
		initComponents();
		loadProperties();
	}
	
    private void loadProperties(){
    	clearProperties();
    	List<Property> allProperties = PropertiesManager.getInstance().getAllProperties();
    	
    	if(allProperties != null && allProperties.size() != 0){
    		for(Property property : allProperties){
    			((DefaultListModel<Property>) lst_properties.getModel()).addElement(property);
    		}
    	}else {
    		tabbedPane.setEnabledAt(1, false);
    		tabbedPane.setEnabledAt(2, false);
    		tabbedPane.setEnabledAt(3, false);
    	}
    	
    	lst_properties.setSelectedIndex(0);
    }
    
    private void clearProperties(){
    	((DefaultListModel<Property>) lst_properties.getModel()).clear();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    	jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lst_properties = new javax.swing.JList<Property>();
        lst_properties.setModel(new DefaultListModel<Property>());
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lst_leases = new javax.swing.JList<Lease>();
        lst_leases.setModel(new DefaultListModel<Lease>());
        tabbedPane = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtArea_propertyReport = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtArea_leaseReport = new javax.swing.JTextArea();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        txtArea_dateRangeReport = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        dc_startDate = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        dc_endDate = new com.toedter.calendar.JDateChooser();
        btn_generate = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        txtArea_tenantReport = new javax.swing.JTextArea();
        cmbo_tenants = new javax.swing.JComboBox<Tenant>();
        cmbo_tenants.setModel(new DefaultComboBoxModel<Tenant>());

        setClosable(true);
        setIconifiable(true);
        setTitle("Report Generator");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Properties", 2, 0));

        lst_properties.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lst_propertiesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lst_properties);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Leases", 2, 0));

        lst_leases.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lst_leasesValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(lst_leases);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setEnabled(false);

        txtArea_propertyReport.setColumns(20);
        txtArea_propertyReport.setEditable(false);
        txtArea_propertyReport.setRows(5);
        txtArea_propertyReport.setFont(new Font("monospaced", Font.PLAIN, 13));
        jScrollPane3.setViewportView(txtArea_propertyReport);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Property Report", jPanel3);

        jPanel4.setEnabled(false);

        txtArea_leaseReport.setColumns(20);
        txtArea_leaseReport.setEditable(false);
        txtArea_leaseReport.setRows(5);
        txtArea_leaseReport.setFont(new Font("monospaced", Font.PLAIN, 13));
        jScrollPane4.setViewportView(txtArea_leaseReport);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Lease Report", jPanel4);

        jPanel5.setEnabled(false);

        txtArea_dateRangeReport.setColumns(20);
        txtArea_dateRangeReport.setEditable(false);
        txtArea_dateRangeReport.setRows(5);
        txtArea_dateRangeReport.setFont(new Font("monospaced", Font.PLAIN, 13));
        jScrollPane5.setViewportView(txtArea_dateRangeReport);

        jLabel1.setText("Create Report from");

        jLabel2.setText(" to");

        btn_generate.setText("Generate");
        btn_generate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_generateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(dc_startDate, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dc_endDate, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn_generate, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btn_generate, 0, 0, Short.MAX_VALUE)
                    .addComponent(dc_endDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dc_startDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Date Range Report", jPanel5);

        jPanel6.setEnabled(false);

        txtArea_tenantReport.setColumns(20);
        txtArea_tenantReport.setEditable(false);
        txtArea_tenantReport.setRows(5);
        txtArea_tenantReport.setFont(new Font("monospaced", Font.PLAIN, 13));
        jScrollPane6.setViewportView(txtArea_tenantReport);

        cmbo_tenants.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbo_tenantsItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(238, 238, 238)
                        .addComponent(cmbo_tenants, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmbo_tenants, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabbedPane.addTab("Tenant Report", jPanel6);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>
    
    private void btn_generateActionPerformed(java.awt.event.ActionEvent evt) {
    	Lease currentLease = getCurrentChosenLease();
    	
    	if(currentLease != null){
    		if(checkDates()){
    			setDateRangeReport(currentLease.generateReport(dc_startDate.getDate(), dc_endDate.getDate()));
    		}else {
    			JOptionPane.showMessageDialog(this, "Invalid date choice, try again", "Invalid Date", JOptionPane.ERROR_MESSAGE);
    		}
    	}
    }
    
    private boolean checkDates(){
		return (dc_startDate.getDate().compareTo(dc_endDate.getDate()) <= 0);
    }
    
    private void cmbo_tenantsItemStateChanged(java.awt.event.ItemEvent evt) {
    	Object tenantObj = cmbo_tenants.getSelectedItem();
		
		if(tenantObj != null){
			setTenantReport(((Tenant) tenantObj).generateReport());
		}
    }
    
    private Property getCurrentChosenProperty(){
    	Object selectedValue = lst_properties.getSelectedValue();
    	
    	if(selectedValue != null){
    		return (Property) selectedValue;
    	}
    	
    	return null;
    }
    
    private Lease getCurrentChosenLease(){
    	Object selectedValue = lst_leases.getSelectedValue();
    	
    	if(selectedValue != null){
    		return (Lease) selectedValue;
    	}
    	
    	return null;
    }
    
    private void lst_propertiesValueChanged(javax.swing.event.ListSelectionEvent evt) {
    	Property currentProperty = getCurrentChosenProperty();
    	
    	if(currentProperty != null){
    		((DefaultListModel<Lease>) lst_leases.getModel()).clear();
    		setPropertyReport(null);
    		setLeaseReport(null);
    		setDateRangeReport(null);
    		setTenantReport(null);
    		tabbedPane.setSelectedIndex(0);
    		tabbedPane.setEnabledAt(1, false);
    		tabbedPane.setEnabledAt(2, false);
    		tabbedPane.setEnabledAt(3, false);
    		loadLeases(currentProperty);
    		createPropertyReport(currentProperty);
    	}
    }
    
    private void lst_leasesValueChanged(javax.swing.event.ListSelectionEvent evt) {
    	Lease currentLease = getCurrentChosenLease();
    	
    	if(currentLease != null){
    		createLeaseReport(currentLease);
    		dc_startDate.setDate(currentLease.getLeaseStartDate());
    		dc_endDate.setDate(currentLease.getLeaseEndDate());
    		setDateRangeReport(null);
//    		dc_startDate.setMinSelectableDate(currentLease.getLeaseStartDate());
//    		dc_startDate.setMaxSelectableDate(currentLease.getLeaseEndDate());
//    		dc_endDate.setMinSelectableDate(currentLease.getLeaseStartDate());
//    		dc_endDate.setMaxSelectableDate(currentLease.getLeaseEndDate());
    		loadTenantReportTenants(currentLease);
    	}
    }
    
    private void setPropertyReport(String propertyReport){
    	txtArea_propertyReport.setText(propertyReport);
    	txtArea_propertyReport.setCaretPosition(0);
    }
    
    private void setLeaseReport(String leaseReport){
    	txtArea_leaseReport.setText(leaseReport);
    	txtArea_leaseReport.setCaretPosition(0);
    }
    
    private void setDateRangeReport(String dateRangeReport){
    	txtArea_dateRangeReport.setText(dateRangeReport);
    	txtArea_dateRangeReport.setCaretPosition(0);
    }
    
    private void setTenantReport(String tenantReport){
    	txtArea_tenantReport.setText(tenantReport);
    	txtArea_tenantReport.setCaretPosition(0);
    }
    
    private void loadLeases(Property property){
    	if(property.getLeases() != null && property.getLeases().size() != 0){
	    	for(Lease lease : property.getLeases()){
	    		((DefaultListModel<Lease>) lst_leases.getModel()).addElement(lease);
	    	}
	    	
	    	tabbedPane.setEnabledAt(1, true);
    		tabbedPane.setEnabledAt(2, true);
	    	lst_leases.setSelectedIndex(0);
    	}
    }

    private void createPropertyReport(Property property){
    	setPropertyReport(property.generateReport());
    }
    
    private void createLeaseReport(Lease lease){
    	setLeaseReport(lease.generateReport());
    }
    
    private void loadTenantReportTenants(Lease lease){
    	((DefaultComboBoxModel<Tenant>) cmbo_tenants.getModel()).removeAllElements();
    	List<Tenant> leaseTenants = lease.getTenants();
    	
    	if(leaseTenants != null && leaseTenants.size() != 0){
	    	for(Tenant tenant : leaseTenants){
	    		((DefaultComboBoxModel<Tenant>) cmbo_tenants.getModel()).addElement(tenant);
	    	}
	    	
	    	tabbedPane.setEnabledAt(3, true);
    	}
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton btn_generate;
    private javax.swing.JComboBox<Tenant> cmbo_tenants;
    private com.toedter.calendar.JDateChooser dc_endDate;
    private com.toedter.calendar.JDateChooser dc_startDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JList<Lease> lst_leases;
    private javax.swing.JList<Property> lst_properties;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JTextArea txtArea_leaseReport;
    private javax.swing.JTextArea txtArea_dateRangeReport;
    private javax.swing.JTextArea txtArea_propertyReport;
    private javax.swing.JTextArea txtArea_tenantReport;
    // End of variables declaration

}
